﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyCode.Utility;
using  BusinessHandler;
public partial class Page_AdvertisementListManagement : System.Web.UI.Page
{
    private int pageSize = 20;
    private int currentPage = 1;
    private AdvertisementItemController advertisementItemController;
    private AdvertisementPositionController advertisementPositionController;
    private AdvertisementCategoryController advertisementCategoryController;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadAdvPosition();
            this.LoadCategory();
            this.LoadList();
        }
    }
    protected void LoadList()
    {
        AdvertisementItemController.Instance = null;
        advertisementItemController = AdvertisementItemController.Instance;
        int? position = null;
        int? category = null;
        DateTime date;
        DateTime? startDate = null;
        DateTime? endDate = null;
        string title = null;
        bool? status = null;
        if (TextBox_Title.Text.Trim() != "")
        {
            title = HttpUtility.HtmlEncode(TextBox_Title.Text.Trim());
        }
        if (this.DropDownList_Category.SelectedIndex > 0)
        {
            category = Protector.Int(this.DropDownList_Category.SelectedValue);
        }
        if (this.DropDownList_Position.SelectedIndex > 0)
        {
            position = Protector.Int(this.DropDownList_Position.SelectedValue);
        }
        if (DateTime.TryParse(this.TextBox_StartDate.Text, out date))
        {
            startDate = date;
        }
        if (DateTime.TryParse(this.TextBox_EndDate.Text, out date))
        {
            endDate = date;
        }
        if (DropDownList_Status.SelectedValue != "")
        {
            status = DropDownList_Status.SelectedValue == "1" ? true : false;
        }
        int skip = (currentPage - 1) * pageSize;
        int total = 0;
        var list = (from item in advertisementItemController.AdvertisementItems
                    where (title == null || item.Name.Contains(title)) &&
                          (category == null || item.AdvCategoryID == category) &&
                          (position == null || item.PositionID == position) &&
                          (startDate == null || item.StartTime == startDate) &&
                          (status == null || item.flag == status) &&
                          (endDate == null || item.EndTime == endDate)
                          orderby item.StartTime descending
                    select item);
        total = list.Count();
        List<AdvertisementItem> newsList = list.Skip(skip).Take(pageSize).ToList();
        if (newsList != null && total > 0)
        {
            tr_not_found.Visible = false;
        }
        else
        {
            tr_not_found.Visible = true;
        }
        Repeater_Content.DataSource = newsList;
        Repeater_Content.DataBind();

        this.BuildPageList(total);
    }

    protected void Repeater_Content_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Protector.Int(e.CommandArgument, 0);
        AdvertisementItemController.Instance = null;
        advertisementItemController = AdvertisementItemController.Instance;
        var c = (from item in advertisementItemController.AdvertisementItems where item.Id == id select item).SingleOrDefault();
        if (c != null)
        {
            if (e.CommandName == "LockUnLockCommand")
            {
                if (!CheckRoles.CanEdit())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }
                c.flag = !c.flag;
                try
                {
                    advertisementItemController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.Advertisement.UpdateSuccess);
                    LoadList();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.Advertisement.ChangeStatusFail + "Error: " + ex.Message);
                }
            }
            else if (e.CommandName == "DeleteCommand")
            {
                if (!CheckRoles.CanDelete())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }
                try
                {
                    advertisementItemController.AdvertisementItems.DeleteOnSubmit(c);
                    advertisementItemController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.News.Deleted);
                    LoadList();
                }
                catch
                {
                    Message_Alert.SetError(Resources.News.DeleteFail);
                }
            }
        }
    }

    protected void Button_Search_Click(object sender, EventArgs e)
    {
        this.LoadList();
    }

    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdvertisementListManagement.aspx");
    }

    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
        string arg = Request.Form["__EVENTARGUMENT"];
        if (string.IsNullOrEmpty(arg)) return;
        if (arg != null)
        {
            string[] temp = arg.Split('|');
            if (temp.Length == 2)
            {
                if (temp[1].ToString().ToUpper() == "PAGE")
                {
                    this.currentPage = Protector.Int(temp[0].ToString(), 0);
                    this.LoadList();
                }
            }
        }
    }

    private void BuildPageList(int numOfRecord)
    {
        string pagingText = "";
        int numOfPage = (numOfRecord / pageSize);
        if ((numOfRecord % pageSize) > 0)
        {
            numOfPage = numOfPage + 1;
        }
        if (numOfPage > 1)
        {
            string pagingLink = Commons.LinkPageForAdmin(numOfPage, this.currentPage, 11, 3);
            pagingText = "<div class='paginator2 right'>" + pagingLink + "</div>";
        }

        Literal_PagingListBot.Text = Literal_PagingListTop.Text = pagingText;
    }
    private void LoadAdvPosition()
    {
        advertisementPositionController = AdvertisementPositionController.Instance;
        this.DropDownList_Position.DataSource = advertisementPositionController.GetPositionList(true);
        this.DropDownList_Position.DataTextField = "Name";
        this.DropDownList_Position.DataValueField = "Id";
        this.DropDownList_Position.DataBind();
        this.DropDownList_Position.Items.Insert(0, new ListItem("--All positions--", "0"));
    }
    private void LoadCategory()
    {
        advertisementCategoryController = AdvertisementCategoryController.Instance;
        this.DropDownList_Category.DataSource = advertisementCategoryController.GetCategoryList(true);
        this.DropDownList_Category.DataTextField = "AdvCatName";
        this.DropDownList_Category.DataValueField = "AdvCatID";
        this.DropDownList_Category.DataBind();
        this.DropDownList_Category.Items.Insert(0, new ListItem("--All categories--", "0"));

    }
}